$(function () {
    template.defaults.imports.dateFormat = function (date) {
        var dt = new Date(date)

        var y = dt.getFullYear()
        var m = dt.getMonth() + 1
        var d = dt.getDate()

        var hh = dt.getHours()
        var mm = dt.getMinutes()
        var ss = dt.getSeconds()

        // 补零
        m = padZero(m)
        d = padZero(d)
        hh = padZero(hh)
        mm = padZero(mm)
        ss = padZero(ss)

        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    }
    // 补零函数
    function padZero (n) {
        return n < 10 ? '0' + n : n
    }

    var q = {
        pagesize: 5,
        pagenum: 1,
        articleid: '',
        uname: '',
    }
    //查询文章ID
    $('#comment-form').on('submit', function (e) {
        e.preventDefault()
        var articleid = $('#articleid').val().trim()
        var uname = $('#uname').val()
        q.articleid = articleid
        q.uname = uname
        // getComment()
        getSearch()
    })
    
    function getSearch(){
        $.ajax({
            method:'get',
            url:'/admin/articleid/1',
            success(res){
                console.log(res);
                var articleid = $('#articleid').val().trim()
                var uname = $('#uname').val()
                q.articleid = articleid
                q.uname = uname
               
            }
            
        })
    }
  $('#btn').on('click',function(){
      $()
  })
    //获取数据
    function getComment() {
        $.ajax({
            method: 'get',
            url: '/admin/comments',
            data: q,
            success: function (res) {
                // console.log(res);
                var htmlStr = template('comment-tpl', res)
                $('tbody').empty().html(htmlStr)
                renderPage(res.total)
            }
        })
    }
    getComment()
    //页码设置
    function renderPage(total) {
        // console.log(total);
        layui.laypage.render({
            elem: 'articlePage', //装页码的容器
            count: total, //数据的总数
            limit: q.pagesize, //每页显示的条数，
            curr: q.pagenum, //起始页
            limits: [1, 2, 4, 6, 8, 10],
            layout: ['limit', 'page', 'prev', 'next', 'count', 'skip'],
            jump: function (obj, first) {
                // console.log('ok');
                /* 
                    first 
                    当生成页码调用了jump,first的值为true,
                    当单击了页码时调用了jump,first的值为undefined.
                */
                // console.log(obj);
                // console.log(first);
                // 判断当前的jump回调函数，是不是单击按钮是调用
                // 这样就不会造成死循环
                // if (first == undefined) {
                if (!first) {
                    // 点击后的obj
                    // console.log(obj);
                    // 获取单击的页码
                    var pagenum = obj.curr;
                    // 修改参数对象q
                    q.pagenum = pagenum;
                    q.pagesize = obj.limit;
                    // 重新获取文章数据
                    getComment()
                }
            }
        })
    }
    // 删除评论
    function deleteComment(id) {
        $.ajax({
            method: 'delete',
            url: '/admin/comments/' + id,
            success: function (res) {
                if (res.status !== 0) return layui.layer.msg('删除失败', {
                    icon: 5,
                    time: 1000
                })
                layui.layer.msg('删除成功', {
                    icon: 6,
                    time: 1000
                })
                getComment()

            }
        })
    }
    $('tbody').on('click', '.delete', function () {
        var id = $(this).attr('data-id')
        localStorage.setItem('id',id)
        layui.layer.confirm('确认全部删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            deleteComment(id)
            layer.close(index);
        });
    })


})